<?php
import('@.ORG.Util.Page'); // 导入分页类

// 本类由系统自动生成，仅供测试用途
class NewEventAction extends BaseAction {
	public function index() {
		header("Content-Type:text/html; charset=utf-8");
		$Event = D('ny_event'); // 实例化User对象
		
		$count = $Event->where("state=1 OR state=0")->count(); // 查询满足要求的总记录数
		$Page = new Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show(); // 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $Event->where("state=1 OR state=0")->order('createDate desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		$this->assign('list', $list); // 赋值数据集
		$this->assign('page', $show); // 赋值分页输出
		$this->display();
	}
	
	public function toAddEvent(){
		$this->display();
	}
	
	public function deploy(){
		$id = $_REQUEST['id'];
		$data['state']=1;
		$model = D("ny_event");
		$model->where("id=".$id)->save($data);
	}
	
	public function addSaveEvent(){
		$data['explain'] = htmlspecialchars($_REQUEST['explain']);
		$data['title'] = htmlspecialchars($_REQUEST['title']);
		$data['key'] = htmlspecialchars($_REQUEST['key']);
		$data['state'] = 0;
		$data['createDate'] = date('Y-m-d H:i:s');
		$model = D("ny_event");
		$id = $model->add($data);
		
		$this->redirect('editEvent', array('id'=>$id));
	}
	
	public function delEvent(){
		$id = $_REQUEST['id'];
		$model = D('ny_event');
		$data['state']=2;
		$model->where("id=".$id)->save($data);
		echo 1;
	}
	
	public function saveEvent(){
		$data['explain'] = htmlspecialchars($_REQUEST['explain']);
		$data['title'] = htmlspecialchars($_REQUEST['title']);
		$data['key'] = htmlspecialchars($_REQUEST['key']);
		$data['id'] = $_REQUEST['id'];
		$model = D("ny_event");
		$model->save($data);
		
		$this->redirect('editEvent', array('id'=>$data['id']));
	}
	
	public function editEvent(){
		$model = D("ny_event");
		$id = $_REQUEST['id'];
		$data = $model->where("id=".$id)->find();
		
		$detailModel = D('ny_event_file');
		$fileList = $detailModel->where("eventId=".$id)->select();
		
		$this->assign("id",$id);
		$this->assign("fileList",$fileList);
		$this->assign("data",$data);
		$this->display();
	}
	
	public function fileUpload() {
		$targetFolder = '/Public/uploads/newEvent/'.date('Ym'); // Relative to the root

		if (!empty ($_FILES)) {
			$tempFile = $_FILES['Filedata']['tmp_name'];

			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
			
			if (!file_exists(rtrim($targetPath, '/'))) {
				//路径不存在就创建
				$this->mkdirs($targetPath);
			}
			$kzm = substr(strrchr($_FILES['Filedata']['name'], '.'), 1);
			$targetFile = rtrim($targetPath, '/') . '/' . time().".".$kzm;
			$path = $targetFolder.'/'.time().".".$kzm;
			// Validate the file type
			$fileTypes = array (
				'jpg',
				'jpeg',
				'gif',
				'png'
			); // File extensions

			$fileParts = pathinfo($_FILES['Filedata']['name']);

			if (in_array($fileParts['extension'], $fileTypes)) {
				if(move_uploaded_file($tempFile, iconv("utf-8", "gb2312", $targetFile))){
					//把文件地址保存到数据
					$model = D("ny_event_file");
					$sort = $model->where("eventId=".$_REQUEST['id'])->max("imgSort");
					$data['imgPath'] = $path;
					$data['eventId'] = $_REQUEST['id'];
					$data['imgSort'] = ($sort+1);
					$newId = $model->add($data);
				}
				
				$result['id'] =  $newId;
				$result['path'] =  $path;
				echo json_encode($result);
			} else {
				echo 'Invalid file type.';
			}
		}
	}
	
	public function delImg(){
		$model = D("ny_event_file");
		$model->where("id=".$_REQUEST['id'])->delete();
	}

	public function mkdirs($dir) {
		if (!is_dir($dir)) {
			if (!$this->mkdirs(dirname($dir))) {
				return false;
			}
			if (!mkdir($dir, 0777)) {
				return false;
			}
		}
		return true;
	}
}